package practice;

import java.util.concurrent.CountDownLatch;

/**
 * @desc: 问题:如何保证多个线程同时启动？来自Java面试题小程序 https://www.javanav.com/interview/93b0069472fd479393006c0e73043fc4.html
 * @author: 王丹
 * @create: 2024-01-11 16:11
 **/
public class TestCountDownLatch {

    private static CountDownLatch countDownLatch = new CountDownLatch(10);

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //await方法是让当前线程等待
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + System.currentTimeMillis());
                }
            });
            t.start();
            //让计数器减1，当计数器变为0时，await方法返回
            countDownLatch.countDown();
        }
    }
}
